package teaching.api;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import teaching.pojo.*;
import teaching.service.PointServise;
import teaching.util.AuthAccess;
import teaching.util.Response;
import teaching.util.ResponseResult;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author:20201002983_郑雨蓓
 * Date: 2024/3/13
 * Description:http://localhost:8080/point/get
 */
@RestController
@RequestMapping("/point")
@Api(tags = "知识点相关接口")
public class PointAPI {
    @Resource
    PointServise pointServise;

    @AuthAccess
    @GetMapping("/get")
    public ResponseResult<Object> getPoint() {
        List<Point> pointList = pointServise.getPoint();
        return Response.createOkResp("查询知识点成功", pointList);
    }

    @GetMapping("/getTag")
    public ResponseResult getTag() {
        List<Tag> tags = pointServise.getTag();
        return Response.createOkResp("标签列表", tags);
    }

    @GetMapping("/{point_id}")
    public ResponseResult getQuestions(@PathVariable("point_id") Long id) {
        List<Questions> questions = pointServise.getQuestionByPoint(id);
        return Response.createOkResp("根据知识点得到问题", questions);
    }


    @GetMapping("/allQuestions")
    public ResponseResult allQuestions() {
        List<Questions> questions = pointServise.showAllQuestions();
        return Response.createOkResp("根据知识点得到问题", questions);
    }

    //    创建知识点
    @PostMapping("/createPoint")
    public ResponseResult<Object> createPoint(Point point) {
        System.out.println(point.toString());
        int res = pointServise.addPoint(point);
        return Response.createMsgResp("创建成功");
    }

    //    根据知识点point1_id或point2_id得到关联的知识点以及关联程度
    @GetMapping("/relation/{pointId}")
    public ResponseResult relationPoint(@PathVariable Long pointId) {
        List<PointRelation> pointRelations = pointServise.findRelatedPoints(pointId);
        return Response.createOkResp(pointRelations);
    }

    @PostMapping("/createQuestion")
    public ResponseResult<Object> createQuestion(@RequestBody Questions questions) {
        try {
            System.out.println(questions.toString());
            int res = pointServise.createQuestion(questions);
            return Response.createOkResp("新建成功");

        } catch (IllegalStateException e) {
            e.printStackTrace();
            return Response.createFailResp("新建失败: " + e.getMessage());
        }
    }

    @PutMapping("/updateCorrelationStrength")
    public ResponseResult<Object> updateCorrelationStrength(@RequestBody List<PointRelation> relations) {
        pointServise.updateCorrelationStrength(relations);
        return Response.createOkResp("修改成功");
    }

    @PostMapping("/createPointRelation")
    public ResponseResult<Object> createPointRelation(@RequestBody PointRelation pointRelation) {
        System.out.println(pointRelation.toString());
        int res = pointServise.createPointRelation(pointRelation);
        return Response.createMsgResp("创建成功");
    }

    @GetMapping("/getPointByTag/{tagId}")
    public ResponseResult<Object> getPointByTag(@PathVariable("tagId") Long tagId) {
        List<Point> pointList = pointServise.getPointByTag(tagId);
        System.out.println(pointList.toString());
        return Response.createOkResp("查询知识点成功", pointList);
    }

}
